package cn.net.sinodata.system.runner;

import cn.net.sinodata.common.core.constant.Constants;
import cn.net.sinodata.common.core.utils.SpringUtils;
import cn.net.sinodata.system.domain.SysQueueThreadPool;
import cn.net.sinodata.system.mapper.SysQueueThreadPoolMapper;
import cn.net.sinodata.threadpool.base.QueueThreadPoolManager;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 初始化 system 模块对应业务数据
 *
 * @author yinliangyun
 */
@Slf4j
@RequiredArgsConstructor
@Component
public class SystemApplicationRunner implements ApplicationRunner {

    @Autowired
    private SysQueueThreadPoolMapper sysQueueThreadPoolMapper;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        log.info("SystemApplicationRunner run start");
        initializeQueueThreadPool();
        log.info("SystemApplicationRunner run end");
    }

    /**
     * 初始化系统线程池
     */
    public void initializeQueueThreadPool() {
        // 查询数据库表sys_queue_thread_pool中所有数据
        List<SysQueueThreadPool> sysQueueThreadPoolList = sysQueueThreadPoolMapper.selectList(null);
        log.info("initializeQueueThreadPool size : {}", sysQueueThreadPoolList.size());

        String xmlName = "";
        String springActiveProfile = SpringUtils.getActiveProfile();
        log.info("initializeQueueThreadPool springActiveProfile : {}", springActiveProfile);

        for (SysQueueThreadPool sysQueueThreadPool : sysQueueThreadPoolList) {
            log.info("initializeQueueThreadPool sysQueueThreadPool : {}", sysQueueThreadPool);
            try {
                // 调用队列线程池管理类初始化线程池
                QueueThreadPoolManager.getInstance().startThreadPool(sysQueueThreadPool.getQueueName(),
                    sysQueueThreadPool.getQueueCode(),
                    sysQueueThreadPool.getQueueClass(),
                    QueueThreadPoolManager.getProcessClass(sysQueueThreadPool.getQueueClass()),
                    sysQueueThreadPool.getThreadSize(),
                    sysQueueThreadPool.getRunStartTime(),
                    sysQueueThreadPool.getRunEndTime(),
                    sysQueueThreadPool.getWaitTime());
            } catch (Exception e) {
                if (Constants.SPRING_ACTIVE_PROFILE_DEV.equals(springActiveProfile) || Constants.SPRING_ACTIVE_PROFILE_SIT.equals(springActiveProfile)) {
                    log.error("initializeQueueThreadPool exception dev or sit sysQueueThreadPool : {}", sysQueueThreadPool + "_" + e.getMessage(), e);
                } else {
                    log.error("initializeQueueThreadPool exception sysQueueThreadPool : {}", sysQueueThreadPool + "_" + e.getMessage(), e);
                    throw e;
                }
            }
        }

        // 获取初始化成功后的线程池个数
        int sysQueueThreadPoolSize = QueueThreadPoolManager.getInstance().getQueueThreadPoolSize();
        log.info("initializeQueueThreadPool end alive sysQueueThreadPoolSize : {}", sysQueueThreadPoolSize);
    }

}
